//
//  AddTelecontrolViewController.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/4.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "AddTelecontrolViewController.h"
#import "MyInfoTableViewCell.h"
#import "TelecontrolTypeViewController.h"
#import "TelecontrolBrandViewController.h"
#import "TelecontrolVersionViewController.h"
#import "InfraredTypeModel.h"
#import "InfraredBrandModel.h"
#import "InfraredVersionModel.h"
#import "InfraredApi.h"
#import "InfraredModel.h"
#import "AddHomeControlApi.h"

@interface AddTelecontrolViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *dataSource;
@property(nonatomic,strong) UITextField *remindTextField;
@property(nonatomic,strong) InfraredTypeModel *typeModel;
@property(nonatomic,strong) InfraredBrandModel *brandModel;
@property(nonatomic,strong) InfraredVersionModel *versionModel;

@end

@implementation AddTelecontrolViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.tableView reloadData];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
//    [self.view endEditing:YES];
    [_remindTextField resignFirstResponder];

}

- (void)onRightButtonClick:(id)sender
{
    if (self.remindTextField.text.length == 0)
    {
        [SVProgressHUD showErrorWithStatus:@"请输入遥控名称"];
        return;
    }
    
    if (!self.versionModel)
    {
        [SVProgressHUD showErrorWithStatus:@"请选择相应的型号"];
        return;
    }
    
    AddHomeControlApi *api = [[AddHomeControlApi alloc] initWithDic:@{@"name":self.remindTextField.text,@"type":self.typeModel.name,@"control_id":self.versionModel.rid}];
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD showSuccessWithStatus:request.responseJSONObject[@"errmsg"]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    } failure:nil];
}

#pragma mark --- tabelview代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return px1920Height(150);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return px1920Height(30);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MyInfoTableViewCell *cell = [MyInfoTableViewCell dequeueReusableWithTableView:tableView];
    if (indexPath.section == 0)
    {
        cell.titleLabel.text = @"类型";
        cell.infoLabel.text = self.typeModel.name;
    }
    else if(indexPath.section == 1)
    {
        cell.titleLabel.text = @"品牌";
        cell.infoLabel.text = self.brandModel.name;
    }
    else
    {
        cell.titleLabel.text = @"型号";
        cell.infoLabel.text = self.versionModel.rmodel;
    }
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [_remindTextField resignFirstResponder];
    __weak typeof(self) weakSelf = self;

    switch (indexPath.section)
    {
        case 0:
        {

            TelecontrolTypeViewController *vc = [TelecontrolTypeViewController new];
            vc.typeBlock = ^(InfraredTypeModel *model) {
                weakSelf.typeModel = model;
            };
            [self.navigationController pushViewController:vc animated:YES];

        }
            break;
        case 1:
        {
            if (!self.typeModel)
            {
                [SVProgressHUD showErrorWithStatus:@"请先选择类型"];
                return;
            }
            TelecontrolBrandViewController *vc = [TelecontrolBrandViewController new];
            vc.type = [NSString stringWithFormat:@"%li",(long)self.typeModel.t];
            vc.brandBlock = ^(InfraredBrandModel *model) {
                weakSelf.brandModel = model;
            };
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        case 2:
        {
            if (!self.brandModel)
            {
                [SVProgressHUD showErrorWithStatus:@"请先选择品牌"];
                return;
            }
            TelecontrolVersionViewController *vc = [TelecontrolVersionViewController new];
            vc.type = [NSString stringWithFormat:@"%li",(long)self.typeModel.t];
            vc.brand = [NSString stringWithFormat:@"%li",(long)self.brandModel.bid];
            vc.versionBlock = ^(InfraredVersionModel *model) {
                weakSelf.versionModel = model;
            };
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
    
    
}

#pragma mark --- 懒加载
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        [superView addSubview:_tableView];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}


- (UITextField *)remindTextField
{
    if(!_remindTextField)
    {
        UIView *superView = self.view;
        _remindTextField = [[UITextField alloc]init];
        [superView addSubview:_remindTextField];
        _remindTextField.placeholder = @"请输入遥控名称";
        _remindTextField.font = fontCommonSize;
        _remindTextField.textColor = [UIColor whiteColor];
        [_remindTextField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        
    }
    return _remindTextField;
}


#pragma mark --- 设置视图布局
- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    

    
    UILabel *tipsLabel = [[UILabel alloc] init];
    [superView addSubview:tipsLabel];
    tipsLabel.text = @"遥控名称";
    tipsLabel.textColor = [UIColor whiteColor];
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.tableView.mas_left);
        make.top.mas_equalTo(superView).with.offset(px1920Height(270));
    }];
    
    [self.remindTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tipsLabel.mas_bottom).with.offset(px1920Height(52));
        make.left.right.mas_equalTo(self.tableView);
    }];
    
    UIView *lineView = ({
        lineView = [[UIView alloc]init];
        [superView addSubview:lineView];
        lineView.backgroundColor = rgba(27,44,81,1);
        [lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.remindTextField.mas_bottom).with.offset(px1920Height(30));
            make.left.right.mas_equalTo(self.tableView);
            make.height.mas_equalTo(1);
        }];
        lineView;
    });
    
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lineView.mas_top).with.offset(px1920Height(121));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(40));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
        make.height.mas_equalTo(px1920Height(510));
    }];
    
}

- (void)setNavigationStyle
{
    [self setNavigationBarTitle:@"添加遥控"];
    [self setRightButtonTitle:@"完成" titleColor:rgba(143,157,189,1) titleFont:fontWithSize(14)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
